package project.dade.cn.bst.fragments;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.pgyersdk.crash.PgyCrashManager;
import com.pgyersdk.javabean.AppBean;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;

import butterknife.ButterKnife;
import butterknife.InjectView;
import project.dade.cn.bst.R;
import project.dade.cn.bst.activity.BrowserActivity;
import project.dade.cn.bst.activity.FeedBackActivity;
import project.dade.cn.bst.base.ParentFragment;
import project.dade.cn.bst.base.ZJPbaseFragment;
import project.dade.cn.bst.utils.Bimp;

/**
 * Created by vain zhou on 2015/9/25.
 */
public class InvestmentFragment extends ParentFragment implements View.OnClickListener {
    @InjectView(R.id.layout_feedback)
    RelativeLayout feedback;
    @InjectView(R.id.layout_checkupdate)
    RelativeLayout checkupdate;
    @InjectView(R.id.layout_clearcache)
    RelativeLayout  clearcache;
    @InjectView(R.id.shareapp)
    RelativeLayout sharepp;

    private KProgressHUD hud;

    public static String FRAGMENT_TAG = InvestmentFragment.class.getSimpleName();

    @Override
    public int setRootViewID() {
        return 0;
    }

    @Override
    public void findView(View rootView) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_more, null);
        ButterKnife.inject(this, v);
        feedback.setOnClickListener(this);
        checkupdate.setOnClickListener(this);
        clearcache.setOnClickListener(this);
        sharepp.setOnClickListener(this);
        clearcache.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_feedback:
                Intent intent =new Intent(getActivity(), FeedBackActivity.class);
                Bimp.drr.clear();
                startActivity(intent);
                break;
            case R.id.layout_checkupdate:
                // 版本检测方式2：带更新回调监听
                PgyUpdateManager.register(getActivity(),
                        new UpdateManagerListener() {
                            @Override
                            public void onUpdateAvailable(final String result) {
                                final AppBean appBean = getAppBeanFromString(result);

                                new AlertDialog.Builder(getActivity())
                                        .setTitle("更新")
                                        .setMessage("")
                                        .setNegativeButton(
                                                "确定",
                                                new DialogInterface.OnClickListener() {

                                                    @Override
                                                    public void onClick(
                                                            DialogInterface dialog,
                                                            int which) {
                                                        startDownloadTask(
                                                                getActivity(),
                                                                appBean.getDownloadURL());
                                                    }
                                                }).show();

                            }

                            @Override
                            public void onNoUpdateAvailable() {
                                Toast.makeText(getActivity(), "已经是最新版本",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
            case R.id.layout_clearcache:
                hud = KProgressHUD.create(getActivity())
                        .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                        .setLabel("清理中..")
                        .setCancellable(true);
                hud.show();
                scheduleDismiss();
                break;
            case R.id.shareapp:
                Intent mIntent = new Intent(getActivity(), BrowserActivity.class);
                mIntent.putExtra("key", BrowserActivity.SHAREAPP);
                startActivity(mIntent);

                break;
        }
    }
    private void scheduleDismiss() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hud.dismiss();
                Toast.makeText(getActivity(), "已清除缓存",
                        Toast.LENGTH_SHORT).show();
            }
        }, 2000);

    }



}

